package com.zhongkai.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.Bean.WebSocketIndBean;
import com.zhongkai.MessageTool;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.IndentService;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.listener.RocketMqProducer;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Indent;
import com.zhongkai.model3.Indent2;
import com.zhongkai.model3.Supplier;
import com.zhongkai.model3.SupplierProduct;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购申请
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IndentServiceImpl implements IndentService {
    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;

/*
    @Autowired
    private JmsTemplate jmsTemplate;

    @Resource
    private Destination topicDestination;*/

    @Autowired
    private RocketMqProducer rocketMqProducer;

    /**
     * 新增采购信息
     *
     * @param uuid        唯一标识
     * @param name        商品名称
     * @param product_uuid 商品uuid
     * @param ind_price   单价
     * @param ind_count   数量
     * @param ind_sum     总金额
     * @param vender_name 厂家名称
     * @param ind_vender  厂家uuid
     * @param ind_person  申请人
     * @param person_uuid 申请人uuid
     */
    @Override
    public ZhongkaiResult addIndentService(String uuid, String name,String product_uuid,
                                           String ind_price, String ind_count, String ind_sum,
                                           String vender_name, String ind_vender,
                                           String ind_person, String person_uuid //申请人 和申请人id
    ) {


        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(ind_price)
                || StringUtils.isNullOrEmpty(ind_count) || StringUtils.isNullOrEmpty(vender_name)
                || StringUtils.isNullOrEmpty(ind_sum) || StringUtils.isNullOrEmpty(ind_vender)
                || StringUtils.isNullOrEmpty(ind_person) || StringUtils.isNullOrEmpty(person_uuid)
                || StringUtils.isNullOrEmpty(product_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }



        Supplier supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.supUuid='" + ind_vender + "'");

        if (supplier == null) {
            return ZhongkaiResult.build(500, "供应商不存在");
        }


        SupplierProduct supplierProduct = (SupplierProduct) iBaseDao.get("from SupplierProduct se where se.proUuid='" + product_uuid + "'");


        if (supplierProduct == null){
            return ZhongkaiResult.build(500, "产品不存在");
        }


        if (!supplierProduct.getSupUuid().equals(supplier.getSupUuid())) {
            return ZhongkaiResult.build(500, "产品与供应商不匹配");
        }


        Indent indent = new Indent();


        indent.setIndUuid(uuid);
        indent.setIndName(name);
        indent.setIndPrice(ind_price);
        indent.setIndCount(ind_count);
        indent.setIndSum(ind_sum);


        indent.setVenderName(supplier.getSupName());
        indent.setIndVender(String.valueOf(supplier.getId()));


        indent.setIndPerson(ind_person);
        indent.setPersonUuid(person_uuid);


        long millis = new DateTime().getMillis(); //获取当前系统时间戳
        indent.setIndTime(millis);//申请时间
        indent.setIndRemove("1");
        indent.setIndState(1); //1待审批  2已审批 3驳回
        indent.setIsPut("1"); //1 未入库  2已入库
        try {
            iBaseDao.save(indent);

            WebSocketIndBean indBean = new WebSocketIndBean();
            indBean.setUser_uuid("null");
            indBean.setType("-1");
            indBean.setContent("您有新的采购申请,请尽快审核~");

            ObjectMapper mapper = new ObjectMapper();

            String json = null;
            try {

                json = MessageTool.FWEB_DATE+mapper.writeValueAsString(indBean);

                byte[] bytes = json.getBytes("utf-8");

                rocketMqProducer.sendMsgIntime(MessageTool.BIG_SKY, MessageTool.FWEB_DATE, bytes);

            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
/*            jmsTemplate.send(topicDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {


                    TextMessage textMessage = session.createTextMessage(json);
                    return textMessage;
                }
            });*/



            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "添加失败");
        }
    }


    /**
     * 修改采购信息
     *
     * @param id        唯一标识
     * @param name        商品名称
     * @param product_uuid 商品uuid
     * @param ind_price   单价
     * @param ind_count   数量
     * @param ind_sum     总金额
     * @param vender_name 厂家名称
     * @param ind_vender  厂家uuid
     * @param ind_person  申请人
     * @param person_uuid 申请人uuid
     */
    @Override
    public ZhongkaiResult updateIndentService(Long id, String name,String product_uuid ,String ind_price, String ind_count, String ind_sum, String vender_name, String ind_vender, String ind_person, String person_uuid) {

        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(ind_price)
                || StringUtils.isNullOrEmpty(ind_count) || StringUtils.isNullOrEmpty(vender_name)
                || StringUtils.isNullOrEmpty(ind_sum) || StringUtils.isNullOrEmpty(ind_vender)
                || StringUtils.isNullOrEmpty(ind_person) || StringUtils.isNullOrEmpty(person_uuid)|| StringUtils.isNullOrEmpty(product_uuid)
                || id == 0
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Indent indent = (Indent) iBaseDao.get("from Indent ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400,"此订单不存在");
        }else if (indent.getIndState() == 2){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }else if ("2".equals(indent.getIsPut())){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }

        Supplier supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.supUuid='" + ind_vender + "'");

        if (supplier == null) {
            return ZhongkaiResult.build(500, "供应商不存在");
        }


        SupplierProduct supplierProduct = (SupplierProduct) iBaseDao.get("from SupplierProduct se where se.proUuid='" + product_uuid + "'");

        if (supplierProduct == null){
            return ZhongkaiResult.build(500, "产品不存在");
        }


        if (!supplierProduct.getSupUuid().equals(supplier.getSupUuid())) {
            return ZhongkaiResult.build(500, "产品与供应商不匹配");
        }


        indent.setIndName(name);
        indent.setIndPrice(ind_price);
        indent.setIndCount(ind_count);
        indent.setIndSum(ind_sum);




        indent.setVenderName(supplier.getSupName());
        indent.setIndVender(String.valueOf(supplier.getId()));



        indent.setIndPerson(ind_person);
        indent.setPersonUuid(person_uuid);

        indent.setIndState(1); //1待审批  2已审批 3 驳回
        indent.setIsPut("1"); //1 未入库  2已入库
        try {
            iBaseDao.update(indent);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "修改失败");
        }
    }

    @Override
    public ZhongkaiResult removeIndentService(Long id) { //删除采购订单
        if (id == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Indent indent = (Indent) iBaseDao.get("from Indent ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400,"此订单不存在");
        }else if (indent.getIndState() == 2){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }else if ("2".equals(indent.getIsPut())){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }


        indent.setIndRemove("2");

        try {
            iBaseDao.update(indent);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "删除失败");
        }
    }

    @Override
    public ZhongkaiResult listIndentService(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Indent.class);
            detachedCriteria.add(Restrictions.eq("indRemove","1")); //增加条件
            detachedCriteria.addOrder(Order.desc("indTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }

    //................................办公采购订单............................

    /**
     * 新增采购信息
     *
     * @param uuid        唯一标识
     * @param name        商品名称
     * @param product_uuid 商品uuid
     * @param ind_price   单价
     * @param ind_count   数量
     * @param ind_sum     总金额
     * @param vender_name 厂家名称
     * @param ind_vender  厂家uuid
     * @param ind_person  申请人
     * @param person_uuid 申请人uuid
     */
    @Override
    public ZhongkaiResult addIndentService2(String uuid, String name,String product_uuid,
                                           String ind_price, String ind_count, String ind_sum,
                                           String vender_name, String ind_vender,
                                           String ind_person, String person_uuid //申请人 和申请人id
    ) {


        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(ind_price)
                || StringUtils.isNullOrEmpty(ind_count) || StringUtils.isNullOrEmpty(vender_name)
                || StringUtils.isNullOrEmpty(ind_sum) || StringUtils.isNullOrEmpty(ind_vender)
                || StringUtils.isNullOrEmpty(ind_person) || StringUtils.isNullOrEmpty(person_uuid) || StringUtils.isNullOrEmpty(product_uuid)

                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }



        Supplier supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.supUuid='" + ind_vender + "'");

        if (supplier == null) {
            return ZhongkaiResult.build(500, "供应商不存在");
        }


        SupplierProduct supplierProduct = (SupplierProduct) iBaseDao.get("from SupplierProduct se where se.proUuid='" + product_uuid + "'");

        if (supplierProduct == null){
            return ZhongkaiResult.build(500, "产品不存在");
        }

        if (!supplierProduct.getSupUuid().equals(supplier.getSupUuid())) {
            return ZhongkaiResult.build(500, "产品与供应商不匹配");
        }


        Indent2 indent = new Indent2();


        indent.setIndUuid(uuid);
        indent.setIndName(name);
        indent.setIndPrice(ind_price);
        indent.setIndCount(ind_count);
        indent.setIndSum(ind_sum);



        indent.setVenderName(supplier.getSupName());
        indent.setIndVender(String.valueOf(supplier.getId()));




        indent.setIndPerson(ind_person);
        indent.setPersonUuid(person_uuid);


        long millis = new DateTime().getMillis(); //获取当前系统时间戳
        indent.setIndTime(millis);//申请时间
        indent.setIndRemove("1");
        indent.setIndState(1); //1待审批  2已审批 3 驳回
        indent.setIsPut("1"); //1 未入库  2已入库
        try {
            iBaseDao.save(indent);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "添加失败");
        }
    }


    /**
     * 修改采购信息
     *
     * @param id          唯一标识
     * @param name        商品名称
     * @param product_uuid 商品uuid
     * @param ind_price   单价
     * @param ind_count   数量
     * @param ind_sum     总金额
     * @param vender_name 厂家名称
     * @param ind_vender  厂家uuid
     * @param ind_person  申请人
     * @param person_uuid 申请人uuid
     */
    @Override
    public ZhongkaiResult updateIndentService2(Long id, String name, String product_uuid,String ind_price, String ind_count, String ind_sum, String vender_name, String ind_vender, String ind_person, String person_uuid) {

        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(ind_price)
                || StringUtils.isNullOrEmpty(ind_count) || StringUtils.isNullOrEmpty(vender_name)
                || StringUtils.isNullOrEmpty(ind_sum) || StringUtils.isNullOrEmpty(ind_vender)
                || StringUtils.isNullOrEmpty(ind_person) || StringUtils.isNullOrEmpty(person_uuid)
                || id == 0|| StringUtils.isNullOrEmpty(product_uuid)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Indent2 indent = (Indent2) iBaseDao.get("from Indent2 ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400,"此订单不存在");
        }else if (indent.getIndState() == 2){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }else if ("2".equals(indent.getIsPut())){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }

        Supplier supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.supUuid='" + ind_vender + "'");

        if (supplier == null) {
            return ZhongkaiResult.build(500, "供应商不存在");
        }


        SupplierProduct supplierProduct = (SupplierProduct) iBaseDao.get("from SupplierProduct se where se.proUuid='" + product_uuid + "'");

        if (supplierProduct == null){
            return ZhongkaiResult.build(500, "产品不存在");
        }

        if (!supplierProduct.getSupUuid().equals(supplier.getSupUuid())) {
            return ZhongkaiResult.build(500, "产品与供应商不匹配");
        }





        indent.setIndName(name);
        indent.setIndPrice(ind_price);
        indent.setIndCount(ind_count);
        indent.setIndSum(ind_sum);




        indent.setVenderName(supplier.getSupName());
        indent.setIndVender(String.valueOf(supplier.getId()));


        indent.setIndPerson(ind_person);
        indent.setPersonUuid(person_uuid);

        indent.setIndState(1); //1待审批  2已审批 3 驳回
        indent.setIsPut("1"); //1 未入库  2已入库
        try {
            iBaseDao.update(indent);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "修改失败");
        }
    }

    @Override
    public ZhongkaiResult removeIndentService2(Long id) { //删除采购订单
        if (id == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Indent2 indent = (Indent2) iBaseDao.get("from Indent2 ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400,"此订单不存在");
        }else if (indent.getIndState() == 2){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }else if ("2".equals(indent.getIsPut())){
            return ZhongkaiResult.build(400,"此订单不可修改");
        }


        indent.setIndRemove("2");

        try {
            iBaseDao.update(indent);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "删除失败");
        }
    }

    @Override
    public ZhongkaiResult listIndentService2(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Indent2.class);
            detachedCriteria.add(Restrictions.eq("indRemove","1")); //增加条件
            detachedCriteria.addOrder(Order.desc("indTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }
}
